<?php require('../../../../wp-config.php'); ?>
<?php if(get_option('canvas_auto_publish') == 'true') $on_update = ', onUpdate:function(){ Canvas.saveAll;}';
		else $on_update = ', onUpdate: Canvas.changePublish'; ?>
Event.observe(window, 'load', function() { Canvas.initialize() }, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var CanvasAjax = Ajax;

var Canvas = {

	addListItem: function(id, name) {
		var count = $F('listcount');
		var elementId = id.parentNode;
		var copiedElement = elementId.cloneNode(true);
		copiedElement.id = name + count;
		copiedElement.firstChild.name = count + 'canvaslist_' + name;
		var parent = elementId.parentNode;
		parent.insertBefore(copiedElement, elementId.nextSibling);
		$('listcount').value++;
	},

	canvasOption: function(name, value) {
		var url = '../wp-content/plugins/Canvas/ajax/canvas-save-option.php';
		var pars = name+'='+value;
		var myAjax = new Ajax.Request(url, 
			{method: 'post', 
			parameters: pars }
			);
	},
	
	changePublish: function() {
		image = $('publish_image');
		if(image) image.src = '../wp-content/plugins/Canvas/images/publish-changed.gif';
	},
	
	createDroppables: function(id) {
		var elements = $$('#'+id+' div.canvas_droppable_zone');
		var shelf = $('shelf');
		Sortable.create('shelf',{tag:'div', only: ['container','container-plugin'], overlap:'horizontal', handle:'handle', constraint:false, containment:elements.concat(shelf), dropOnEmpty:true<?php echo $on_update; ?>});
		elements.each(function(item){
			Sortable.create(item.id,{tag:'div', only: ['container','container-plugin'], overlap:'horizontal', handle:'handle', constraint:false, containment:elements.concat(shelf), dropOnEmpty:true<?php echo $on_update; ?>});
		});
	},
	
	destroyDroppables: function(id) {
		Sortable.destroy('shelf');
		$$('#'+id+' div.canvas_droppable_zone').each(function(item){
			Sortable.destroy(item.id);
		});
	},
	
	exportXml: function(pars) {
		$('export_message').className = "exporting";
		var target = 'export_message';
		var url = '../wp-content/plugins/Canvas/ajax/canvas-export.php';
		var myAjax = new Ajax.Updater({success: target}, url, 
			{method: 'post', 
			parameters: pars,
			onComplete: function() { $('export_message').className = ''; $('export_message').style.paddingLeft = '0px'; }
			});
	},
	
	gallerySwitch: function(id) {
		if($F('selected_image') != '') $($F('selected_image')).className = '';
		$(id).className = 'selected_image';
		$('selected_image').value = id;
		$('path').value = $F('directory')+id;
	},
	
	importXml: function() {
		var pars = 'path_to_xml='+$F('path_to_xml');
		$('export_message').className = "importing";
		var target = 'export_message';
		var url = '../wp-content/plugins/Canvas/ajax/canvas-import.php';
		var myAjax = new Ajax.Updater({success: target}, url, 
			{method: 'post', 
			parameters: pars,
			onComplete: function() { $('export_message').className = ''; $('export_message').style.paddingLeft = '0px'; }
			});
	},
	
	initialize: function() {
		// This seems to be causing problems with the latest release of WP - in Canvas admin mode
		var bigdiv = document.createElement('div');
		document.body.insertBefore(bigdiv, $('wpwrap'));
		bigdiv.appendChild($('wphead'));
		bigdiv.appendChild($('adminmenu'));
		bigdiv.appendChild($('dashmenu'));
		bigdiv.appendChild($('sidemenu'));
		bigdiv.appendChild($('submenu'));
		bigdiv.appendChild($('user_info'));
	
		var hide_instructions = $('hide_instructions');
		if(hide_instructions) {
			hide_instructions.onclick = function(){ 
				Canvas.canvasOption('canvas_show_instructions','false');
				new Effect.Fade('instructions', { duration: 0.4 });
				};
		}
		$$('ul.page_dropdown ul li a').each(function(item){
			$(item).onclick = function(){ Canvas.switchCanvas(item.id) };
		});
		$('show_content').onclick = function(){ Canvas.toggleMenu('show_content', 'hide_content', bigdiv); };
		$('hide_content').onclick = function(){ Canvas.toggleMenu('show_content', 'hide_content', bigdiv); };
		$$('div.titlebar a img').each(function(item){
			$(item).onclick = function(){ Canvas.sortGroups(item.id) };
		});
		$$('.canvas_droppable_zone h8').each(function(item){
			$(item).onclick = function(){ Canvas.toggleZoneOptions(item.id) };
		});
		<?php if(get_option('canvas_resize_shelf') == 'true') echo "if($('leftcolumn')) new Resizeable('leftcolumn');"; ?>
		<?php if(get_option('canvas_hide_menu') == 'true') echo "Canvas.toggleMenu('show_content', 'hide_content', bigdiv);";
				else echo '$("show_content").style.display  = "none";	$("hide_content").style.display = "inline";'; ?>
		if($('canvas_page')) {
			mainpage = $F('canvas_page');
			$(mainpage).style.display = "block";
			var mainmenu = $(mainpage+'__menu');
			if(mainmenu) { mainmenu.parentNode.className = 'selected'; }
			var publish = $('publish');
			if(publish) { publish.onclick = Canvas.saveAll; }
			Canvas.createDroppables(mainpage);
		}
	},
	
	removeListItem: function(id) {
		if(window.confirm('Are you sure you want to remove this item?')) {
			new Effect.Fade(id.parentNode,{ duration: 0.6, afterFinish: function(){ Element.remove(id.parentNode); } });
		}
	},

	restorePublish: function() {
		image = $('publish_image');
		if(image) image.src = '../wp-content/plugins/Canvas/images/publish.gif';
	},
	
	saveAll: function() {
		if($('publish')) { $('publish').style.backgroundImage = "url('../wp-content/plugins/Canvas/images/spinner.gif')"; }
		var layout_params;
		$$('#'+$F('canvas_page')+' div.canvas_droppable_zone').concat($('shelf')).each(function(item){
			layout_params = Canvas.serializeHelper(layout_params, item.id);
		});
		Canvas.saveLayout(layout_params);
		Canvas.zoneOptions();
	},
	
	saveBlock: function(element, pars) {
		var url = '../wp-content/plugins/Canvas/ajax/canvas-save-plugin.php';
		var myAjax = new Ajax.Request(url, 
			{method: 'get', 
			parameters: pars }
			);
	},
	
	saveLayout: function(pars) {
		var url = '../wp-content/plugins/Canvas/ajax/canvas-save-layout.php';
		var myAjax = new Ajax.Request(url, 
			{method: 'get', 
			parameters: pars
			});
	},
		
	saveZone: function(pars) {
		var url = '../wp-content/plugins/Canvas/ajax/canvas-save-zone-options.php';
		var myAjax = new Ajax.Request(url, 
			{method: 'get', 
			parameters: pars,
			onComplete: function(){ if($('publish')){ $('publish').style.backgroundImage = "url('')"; Canvas.restorePublish(); } }
			});
	},

	serializeHelper: function(layout_params, block) {
		layout_params = Try.these (
			function() {
				  content_exists = Sortable.serialize(block);
				  if (content_exists == '') {
					  return layout_params;
				  } else if (typeof(layout_params) == 'undefined')  {
					  return content_exists;
				  } else {
					  return layout_params + '&' + content_exists;
				  }
			}
		);
		return layout_params;
	},
	
	sortGroups: function(group) {
		if(group == 'all') {
			$$('div.shelf_column div').each(function(item){
				$(item).style.display = 'block';
			});
		} else {
			$$('div.shelf_column div.container').each(function(item){
				$(item).style.display = 'none';
			});
			$$('div.shelf_column div.container-plugin').each(function(item){
				$(item).style.display = 'none';
			});
			$$('div.shelf_column div.'+group).each(function(item){
				$(item).style.display = 'block';
			});
		}
	},
	
	switchCanvas: function(element) {
		var currentPage = $F('canvas_page');
		$$('ul.page_dropdown ul li').each(function(item){ if($(item).className == 'selected') $(item).className = ''; });
		$(element).parentNode.className = 'selected';
		var string = element.split('__');
		$(currentPage).style.display = 'none';
		Canvas.destroyDroppables(currentPage);
		$(string[0]).style.display = 'block';
		$('canvas_page').value = string[0];
		Canvas.createDroppables(string[0]);
	},
	
	toggleMenu: function(show,hide,block) {
		if($(block).style.display == "none") {
			$(show).style.display  = "none";
			$(hide).style.display = "inline";
			new Effect.BlindDown(block, {duration: 0.3 })
		} else {
			$(show).style.display  = "inline";
			$(hide).style.display = "none";
			new Effect.BlindUp(block, {duration: 0.3 })
		}
	},
	
	toggleZoneOptions: function(id) {
		if($(id.replace('togglezoneoption_','options_')).style.display == 'block') {
			$(id.replace('togglezoneoption_','options_')).style.display = 'none';
			$(id).style.backgroundColor = '#CFCFCF';
		} else {
			$(id.replace('togglezoneoption_','options_')).style.display = 'block';
			$(id).style.backgroundColor = 'transparent';
		}
	},
	
	zoneHandler: function(zone, position) {
		$$('span#options_' + zone + ' img').each(function(item){
			item.src = item.src.replace('_down', '_up');
			item.src = item.src.replace(position+'_up', position+'_down');
		});
		$('zoneoption_'+zone).value = position;
		Canvas.changePublish();
	},

	zoneOptions: function() {
		string = '';
		$$('#'+$F('canvas_page')+' select.zone_content_options').each(function(item){
			string = string + '&' + item.id + '=' + $F(item.id);
		});
		Canvas.saveZone(string);
	}
};


// This code was found here: http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}